// File Name: RotatePatternHandler.cpp

#include "StdAfx.h"
#include "RotatePatternHandler.h"
#include "ICustomProvider.h"
#include "RotatePatternClientWrapper.h"

namespace UIAutomationCPP
{
	RotatePatternHandler::RotatePatternHandler()
	{
	}

	RotatePatternHandler::~RotatePatternHandler()
	{
	}

	STDMETHODIMP RotatePatternHandler::CreateClientWrapper(IUIAutomationPatternInstance *pPatternInstance, IUnknown **pClientWrapper)
	{
		*pClientWrapper = new RotatePatternClientWrapper(pPatternInstance);
		if (*pClientWrapper == NULL)
			return E_INVALIDARG;
		return S_OK;
	}
	
	STDMETHODIMP RotatePatternHandler::Dispatch(IUnknown *pTarget, UINT index, const struct UIAutomationParameter *pParams, UINT cParams)
	{
		switch(index)
		{
			case Rotation_GetIsReadOnly:
				return ((ICustomProvider*)pTarget)->get_IsReadOnly((BOOL*)pParams[0].pData);
			case Rotation_GetValue:
				return ((ICustomProvider*)pTarget)->get_Value((BSTR*)pParams[0].pData);
			case Rotation_SetValue:
				return ((ICustomProvider*)pTarget)->SetValue(*(LPCWSTR*)pParams[0].pData);
			case Rotation_Reset:
				return ((ICustomProvider*)pTarget)->Reset();
		}
		return E_INVALIDARG;
	}

	HRESULT __stdcall RotatePatternHandler::QueryInterface(REFIID riid, void **ppvObj)//riid == IID_IUIAutomationRegistrar, ppvObj == interface pointer to registrar
	{
		if(riid == IID_IUnknown)
			*ppvObj =(IUnknown*)((IUIAutomationPatternHandler*)this);
		else if(riid == IID_IUIAutomationPatternHandler)
			*ppvObj =(IUIAutomationPatternHandler*)this;
		else
		{
			*ppvObj = NULL;
			return E_NOINTERFACE;
		}

		((IUnknown*)(*ppvObj))->AddRef();
		return S_OK;
	}

	ULONG __stdcall RotatePatternHandler::AddRef()
	{
		InterlockedIncrement(&refCount);
		return refCount;
	}

	ULONG __stdcall RotatePatternHandler::Release()
	{
		ULONG ulRefCount = InterlockedDecrement(&refCount);
		if (ulRefCount == 0)
		{
			delete this;
		}
		return ulRefCount;
	}
}
